package com.tsing.bms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.bms.dto.NameAndPhoneDto;
import com.tsing.bms.entity.House;
import com.tsing.bms.entity.Order;
import com.tsing.bms.mapper.HouseMapper;
import com.tsing.bms.mapper.OrderMapper;
import com.tsing.bms.service.IHouseService;
import com.tsing.bms.vo.req.*;
import com.tsing.bms.vo.resp.HouseRageRespVo;
import com.tsing.bms.vo.resp.HouseRespVo;
import com.tsing.bms.vo.resp.IdAndNameRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements IHouseService {

    @Override
    public void addHouse(HouseAddReqVo reqVo) {
//        if (0 != baseMapper.selectCount(new QueryWrapper<House>().eq("name", reqVo.getName()))) {
//            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
//        }
        House house = new House();
        BeanUtils.copyProperties(reqVo, house);
        baseMapper.insert(house);
    }

    @Override
    public void deleteHouse(IdsReqVo reqVo) {
        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
            throw new BusinessException(ServerException.Proxy.HOUSE_HAVE_RELEVANCE);
        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public HouseRespVo getHouseById(IdReqVo reqVo) {
        HouseRespVo houseRespVo = new HouseRespVo();
        BeanUtils.copyProperties(baseMapper.selectById(reqVo.getId()), houseRespVo);
        return houseRespVo;
    }

    @Override
    public void updateHouse(HouseUpdateReqVo reqVo) {
//        if (0 != baseMapper.selectCount(new QueryWrapper<House>().eq("name", reqVo.getName())
//                .ne("id", reqVo.getId()))) {
//            throw new BusinessException(ServerException.Proxy.NAME_HAVE_RELEVANCE);
//        }
        House house = new House();
        BeanUtils.copyProperties(reqVo, house);
        baseMapper.updateById(house);
    }

    @Override
    public Page<HouseRageRespVo> housePage(HousePageReqVo reqVo) {
        Page<House> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
        List<HouseRageRespVo> list = baseMapper.housePage(reqVo, page);
        list.forEach(e -> {
            if (StrUtil.isNotBlank(e.getHouseholdId())) {
                NameAndPhoneDto nameAndPhoneDto = baseMapper.selectZhuhu(e.getHouseholdId());
                e.setHouseholdName(nameAndPhoneDto.getName());
                e.setHouseholdPhone(nameAndPhoneDto.getPhone());
            }
        });
        Page<HouseRageRespVo> housePage = new Page<>();
        housePage.setTotal(page.getTotal());//总条数
        housePage.setCurrent(page.getCurrent());//页码
        housePage.setSize(page.getSize());//当前页个数
        housePage.setRecords(list);//当前页数据
        return housePage;
    }

    @Resource
    private OrderMapper orderMapper;

    @Override
    @Transactional
    public void updateBalance(UpdateBalanceReqVo reqVo) {
        House house = new LambdaQueryChainWrapper<>(baseMapper)
                .select(House::getBalance)
                .eq(House::getId, reqVo.getId())
                .one();
        new LambdaUpdateChainWrapper<>(baseMapper)
                .set(House::getBalance, reqVo.getBalance().add(house.getBalance()))
                .eq(House::getId, reqVo.getId())
                .update();
        Order order = new Order()
                .setHouseId(reqVo.getId())
                .setType(0)
                .setPrice(reqVo.getBalance())
                .setDetail("管理员充值 " + reqVo.getBalance() + " 元");
        orderMapper.insert(order);
    }

    @Override
    public List<IdAndNameRespVo> userList(NameReqVo reqVo) {
        return baseMapper.userList(reqVo.getName());
    }

}
